package com.chap16.immutability;

/**
 * <p>
 * <code>Demo2</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-25 15:44
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Demo2 {
    public static void main(String[] args) {
        SmsInfo smsInfo = new SmsInfo("https://www.aliyun.com",180);

        //修改短信服务商的信息
        new Thread(()->{
            //不会出现问题。这是里new了一个新的对象。另外线程读的是副本
            smsInfo.update("https://cloud.tencent.com", 200);
        },"线程1").start();


        //读取短信服务商的信息
        new Thread(()->{
            System.out.println("获取短信服务商信息："+smsInfo);
        },"线程2").start();
    }


    private static final class  SmsInfo{
        //短信服务商信息
        //服务商地址
        private  final String url;
        //短信字节数
        private final Integer maxSizeInBytes;

        public SmsInfo(String url, Integer maxSizeInBytes) {
            this.url = url;
            this.maxSizeInBytes = maxSizeInBytes;
        }

        public String getUrl() {
            return url;
        }


        public Integer getMaxSizeInBytes() {
            return maxSizeInBytes;
        }

        public SmsInfo update(String url,Integer maxSizeInBytes){
            return new SmsInfo(url,maxSizeInBytes);
        }
        @Override
        public String toString() {
            return "SmsInfo{" +
                    "url='" + url + '\'' +
                    ", maxSizeInBytes=" + maxSizeInBytes +
                    '}';
        }
    }
}
